<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>注册 - 简易邮件管理系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: Arial, sans-serif;
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            background-color: #f3f3f3;
        }
        .container {
            width: 90%;
            max-width: 500px;
            background-color: white;
            border-radius: 10px;
            padding: 20px 30px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            text-align: center;
        }
        h1 {
            font-size: 28px;
            color: #333;
            margin-bottom: 20px;
        }
        form {
            display: flex;
            flex-direction: column;
            gap: 15px;
        }
        input {
            width: 100%;
            padding: 12px;
            font-size: 16px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        button {
            padding: 12px 20px;
            font-size: 16px;
            color: white;
            background-color: #007bff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        button:hover {
            background-color: #0056b3;
        }
        .links {
            margin-top: 20px;
            font-size: 14px;
        }
        .links a {
            color: #007bff;
            text-decoration: none;
            margin: 0 10px;
        }
        .links a:hover {
            text-decoration: underline;
        }
        .popup {
            display: none;
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            padding: 20px;
            background-color: white;
            border: 1px solid #ddd;
            border-radius: 5px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            z-index: 1000;
        }
        .popup button {
            margin-top: 10px;
        }
    </style>
    <script>
        function handleFormSubmit(event) {
            event.preventDefault();
            const form = event.target;
            const formData = new FormData(form);
            fetch(form.action, {
                method: form.method,
                body: formData
            }).then(response => response.json())
              .then(data => {
                  if (data.message === '注册成功') {
                      document.getElementById('success-popup').style.display = 'block';
                  } else {
                      document.getElementById('error-popup').style.display = 'block';
                      document.getElementById('error-message').innerText = data.message;
                  }
              }).catch(error => {
                  document.getElementById('error-popup').style.display = 'block';
                  document.getElementById('error-message').innerText = error.message;
              });
        }

        function closePopup(id) {
            document.getElementById(id).style.display = 'none';
            if (id === 'success-popup') {
                window.location.href = "{{ url_for('main.login') }}";
            }
        }
    </script>
</head>
<body>
    <div class="container">
        <h1>注册</h1>
        <!-- 注册表单 -->
        <form action="/register" method="post" onsubmit="handleFormSubmit(event)">
            <input type="text" name="username" placeholder="用户名" required>
            <input type="password" name="password" placeholder="密码" required>
            <input type="email" name="email" placeholder="电子邮箱" required>
            <input type="tel" name="cellphone" placeholder="电话" required>
            <input type="text" name="address" placeholder="地址" required>
            <button type="submit">注册</button>
        </form>
        <div class="links">
            <a href="{{ url_for('main.login') }}">已有账号？登录</a>
            <a href="{{ url_for('main.index') }}">返回主页</a>
        </div>
    </div>
    <!-- 成功弹窗 -->
    <div id="success-popup" class="popup">
        <p>注册成功！</p>
        <button onclick="closePopup('success-popup')">关闭</button>
    </div>
    <!-- 错误弹窗 -->
    <div id="error-popup" class="popup">
        <p id="error-message">注册失败。</p>
        <button onclick="closePopup('error-popup')">关闭</button>
    </div>
</body>
</html>
